<?php

namespace Biz\model\wiki;

use Biz\service\brand\Brand;
use Doraemon\pockets\datebase\BaseDbModel;
use Medoo\Medoo;

class Wiki extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki';

    const STATUS_NORMAL = 0;//待完善资料
    const STATUS_WAIT_CHECK = 1;//待审核
    const STATUS_CHECKED = 2;//已审核
    const STATUS_DEL = 9;//已删除

    const CHANNEL_TAOBAO = 1;
    const CHANNEL_JD = 2;
    const CHANNEL_SUNING = 4;
    const CHANNEL_KAOLA = 5;
    const CHANNEL_YOUZAN = 6;

    const CHANNEL_HAOHAOZHU = 7;
    const CHANNEL_XIAOEPINPIN = 8;
    const CHANNEL_AMAZON = 9;
    const CHANNEL_XIAOMIYOUPIN = 10;
    const CHANNEL_SAMSUNGESHOP = 12;

    const CHANNEL_NOTHING = 11;

    const CHANNEL_MAP = [
        self::CHANNEL_TAOBAO => '淘宝',
        self::CHANNEL_JD => '京东',
        self::CHANNEL_SUNING => '苏宁',
        self::CHANNEL_KAOLA => '网易考拉',
        self::CHANNEL_YOUZAN => '有赞',
        self::CHANNEL_XIAOEPINPIN => '小鹅拼拼',
        self::CHANNEL_AMAZON => '亚马逊',
        self::CHANNEL_XIAOMIYOUPIN => '小米有品',
        self::CHANNEL_HAOHAOZHU => '好好住',
        self::CHANNEL_NOTHING => '虚拟商品',
        self::CHANNEL_SAMSUNGESHOP => '三星'
    ];

    const CHANNEL_TYPE_TAOBAO = 1;
    const CHANNEL_TYPE_TMALL = 2;
    const CHANNEL_TYPE_JD = 3;
    const CHANNEL_TYPE_SUNING = 4;
    const CHANNEL_TYPE_KAOLA = 5;
    const CHANNEL_TYPE_YOUZAN = 6;
    const CHANNEL_TYPE_XIAOEPINPIN = 8;
    const CHANNEL_TYPE_AMAZON = 9;
    const CHANNEL_TYPE_XIAOMIYOUPIN = 10;
    const CHANNEL_TYPE_SAMSUNGESHOP = 12;

    const CHANNEL_TYPE_MAP = [
        self::CHANNEL_TYPE_TAOBAO => '淘宝',
        self::CHANNEL_TYPE_TMALL => '天猫',
        self::CHANNEL_TYPE_JD => '京东',
        self::CHANNEL_TYPE_SUNING => '苏宁',
        self::CHANNEL_TYPE_KAOLA => '网易考拉',
        self::CHANNEL_TYPE_YOUZAN => '有赞',
        self::CHANNEL_TYPE_XIAOEPINPIN => '小鹅拼拼',
        self::CHANNEL_TYPE_AMAZON => '亚马逊',
        self::CHANNEL_XIAOMIYOUPIN => '小米有品',
        self::CHANNEL_NOTHING => '虚拟商品',
        self::CHANNEL_TYPE_SAMSUNGESHOP => '三星'
    ];

    const ADM_STATUS_MAP = [
        self::STATUS_NORMAL => '待完善资料',
        self::STATUS_WAIT_CHECK => '待审核',
        self::STATUS_CHECKED => '已审核',
        self::STATUS_DEL => '已下架',
    ];

    //wiki第三方商品状态 1:上线 0：下线
    public const CHANNEL_SPU_STATUS_ON = 1;
    public const CHANNEL_SPU_STATUS_OFF = 0;
    //wiki第三方商品状态对应值
    public const CHANNEL_STATUS_ARR = [
        self::CHANNEL_SPU_STATUS_ON => "有效",
        self::CHANNEL_SPU_STATUS_OFF => "失效",
    ];

    const IS_BLACK_CARD = 1;
    const NOT_BLACK_CARD = 0;

    const IS_DEFAULT = 1;
    const IS_OFFICIAL_Y = 1; //官方商品


    public static function getInfoByChannelSpu($channel, $channelSpuId)
    {
        $wikiInfo = self::getByWhere([
            'channel' => $channel,
            'channel_spu_id' => $channelSpuId,
        ]);

        return $wikiInfo;
    }

    public static function getByWhere($where, $field = '*')
    {
        return self::get($field, $where);
    }

    public static function getInfoByWikiId($wikiId)
    {
        $where = [
            'id' => $wikiId,
            'status[!]' => self::STATUS_DEL,
        ];
        $info = self::getByWhere($where);

        return $info;
    }

    public static function getInfosByWikiIds($wikiIds, $fields = '*')
    {
        $result = [];
        $where = [
            'id' => $wikiIds,
            'status[!]' => self::STATUS_DEL,
        ];
        $list = self::selectByWhere($where, $fields);
        if ($list) {
            foreach ($list as $row) {
                $result[$row['id']] = $row;
            }
        }

        return $result;


    }

    public static function getInfosByBrandIds($brandIds, $fields = '*')
    {
        $result = [];
        $where = [
            'brand_id' => $brandIds,
            'status[!]' => self::STATUS_DEL,
        ];
        $list = self::selectByWhere($where, $fields);
        foreach ($list as $row) {
            $result[$row['id']] = $row;
        }

        return $result;
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);

        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);

        return $count;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    public static function hasWikiIds(array $wikiIds)
    {
        if ($wikiIds) {
            $hasWikiIds = self::select('id', [
                'id' => $wikiIds,
                'status[!]' => self::STATUS_DEL,
            ]);
        } else {
            $hasWikiIds = [];
        }
        $Rst = [];
        foreach ($wikiIds as $wikiId) {
            $Rst[$wikiId] = in_array($wikiId, $hasWikiIds) ? 1 : 0;
        }

        return $Rst;
    }

    /**
     * 根据basic Id 查询默认商品
     * @param $basicIds
     * @param string $select
     * @return array|bool
     */
    public static function getDefaultListByBasicId($basicIds, $select = '*')
    {
        $where = [
            'status' => self::STATUS_CHECKED,
            'basic_id' => $basicIds,
            'is_default' => 1,
            'is_official' => 1,
        ];
        $list = self::selectByWhere($where, $select);

        return $list;
    }


    /**
     * 根据basic Ids 查询默认商品
     * @param $basicIds
     * @param string $select
     * @return array|bool
     */
    public static function getDefaultWikiInfoByBasicIds($basicIds, $select = '*')
    {
        $where = [
            'status[!]' => self::STATUS_DEL,
            'basic_id' => $basicIds,
            'is_default' => 1,
        ];
        return self::selectByWhere($where, $select);
    }

    /**
     * 根据basic Id 查询默认商品
     * @param $basicId
     * @param string $select
     * @return array|bool
     */
    public static function getInfoByBasicId($basicId, $select = '*')
    {
        $where = [
            'status[!]' => self::STATUS_DEL,
            'basic_id' => $basicId,
            'is_default' => 1,
        ];
        return self::getByWhere($where, $select);
    }

    /**
     * 通过basic_id获取品牌默认商品
     * @param $basicIds
     * @param $brand_id
     * @param string $select
     * @return array|bool
     */
    public static function getBrandDefaultWikiByBasicId($basicIds, $brand_id, string $select = '*')
    {
        $where = [
            'status[!]' => self::STATUS_DEL,
            'basic_id' => $basicIds,
            'brand_id' => $brand_id,
            'is_default' => 1,
            'is_official' => 1,
        ];
        return self::selectByWhere($where, $select);
    }


    /**
     * 通过basic_id获取品牌默认商品
     * @param $basicId
     * @param $brand_id
     * @param string $select
     * @return array|bool
     */
    public static function getDefaultWikiByBasicId($basicId, $brand_id, string $select = '*')
    {
        $where = [
            'status[!]' => self::STATUS_DEL,
            'basic_id' => $basicId,
            'brand_id' => $brand_id,
            'is_default' => 1,
            'is_official' => 1,
        ];
        return self::getByWhere($where, $select);
    }

    /**
     * 获取品牌标签
     * @param $where
     * @param string $select
     * @return array|bool
     */
    public static function getBrand($where, $select = '*')
    {
        $list = self::select($select, $where);
        if (!empty($list)) {
            foreach ($list as &$item) {
                $item['uid'] = 0;
                $item['brand_name'] = $item['channel_brand'];
                $item['logo_url'] = Brand::BRAND_DEFAULT_LOGO;
            }
            return $list;
        }
        return [];
    }


    /**
     * 根据basic_id获取默认商品信息
     * @param $wheres
     * @param string $select
     * @return array|mixed
     */
    public static function getDefaultInfoByBasicId($wheres, $select = '*')
    {
        $where = [
            'status[!]' => self::STATUS_DELETE,
            'basic_id' => $wheres['basic_id'],
            'brand_id' => $wheres['brand_id'],
            'is_default' => 1,
        ];

        return self::get($select, $where);
    }

    /**
     * 分组统计商品数和wiki数
     * @param $brandArr
     * @param false $isWiki
     * @return array
     */
    public static function getGroupCountByBrandArr($brandArr, $isWiki = false): array
    {
        $where['status[!]'] = self::STATUS_DELETE;
        $where['brand_id'] = $brandArr;
        if ($isWiki) {
            $where['is_default'] = self::IS_DEFAULT;
            $where['is_official'] = self::IS_OFFICIAL_Y;
        }
        $where['GROUP'] = 'brand_id';
        $data = self::select(['count' => Medoo::raw('count(brand_id)'), 'brand_id'], $where);
        $res = [];
        if ($data) {
            foreach ($data as $val) {
                $res[$val['brand_id']] = $val['count'];
            }
        }

        return $res;
    }

    /**
     * 分组计算品牌对应商品的关联内容数量以及购买次数
     * @param $brandArr
     * @param $column
     * @return array
     */
    public static function getSumByBrandArr($brandArr, $column): array
    {
        $where['status[!]'] = self::STATUS_DELETE;
        $where['brand_id'] = $brandArr;
        $where['GROUP'] = 'brand_id';
        $data = self::select(['sum' => Medoo::raw("sum($column)"), 'brand_id'], $where);
        $res = [];
        if ($data) {
            foreach ($data as $val) {
                $res[$val['brand_id']] = $val['sum'];
            }
        }

        return $res;
    }

    /**
     * 根据wiki_id 获取淘宝商品id
     * @param array $wikiIds
     * @return array
     */
    public static function getTbItemIdByWikiIds(array $wikiIds): array
    {
        if (empty($wikiIds)) {
            return [];
        }
        $where = [
            'id' => $wikiIds,
            'channel' => self::CHANNEL_TAOBAO,
            'status[!]' => self::STATUS_DEL,
            'channel_spu_status' => self::CHANNEL_SPU_STATUS_ON,
        ];

        return self::select(['channel_spu_id', 'id'], $where) ?? [];
    }
}